﻿using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using WorkFlowCore.Authorization;
using WorkFlowCore.Framework.UserSelectors;
using WorkFlowCore.UserSelectors;

namespace WorkFlowCore.Framework.Authorization
{
    public class DefaultSession : IWorkflowSession
    {
        public DefaultSession(IHttpContextAccessor httpContextAccessor)
        {
            //TODO通过请求上下文获取用户凭据
            
            var user = UserList.Users[0];
            
            try
            {
                if (httpContextAccessor.HttpContext == null) return;
                var userInfoStr = httpContextAccessor.HttpContext.Request.Headers["user-info"];
                userInfoStr = WebUtility.UrlDecode(userInfoStr);
                this.User = JsonConvert.DeserializeObject<User>(userInfoStr);
            }
            catch (Exception)
            {
                this.User = new User(user.Id, user.Name);
            }
            //此处不做具体实现。模拟返回其中一个用户
            
        }

        public User User { get; private set; }
    }
}
